<?php
namespace App\Http\Controllers;

use DateTime;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Http;
use Carbon\Carbon;


class WorkUserStatisticsController extends Controller
{

    public function workUserStatistics(Request $request)
    {
        $data = $request->all();
        $start = $data['start'];
        $end = $data['end'];
        $engineering = $data['engineering'];


        $res = [];

        if(empty($engineering)){
            $res['subjects'] = DB::table('work_user_history')
                ->where(function ($query) use ($start,$end) {
                    $query->whereBetween('enter_time',[$start,$end])
                        ->orWhereBetween('exit_time',[$start,$end]);
                })
                ->where('deleted_at',null)

                ->select('enter_time','type','engineering','exit_time', DB::raw('count(*) as num'))
                ->groupBy('enter_time','exit_time','type')->orderBy('enter_time','desc')->get()->map(function ($value) {return (array)$value;})->toArray();

        }else{
            $res['subjects'] = DB::table('work_user_history')
                ->where(function ($query) use ($start,$end) {
                    $query->whereBetween('enter_time',[$start,$end])
                        ->orWhereBetween('exit_time',[$start,$end]);
                })
                ->where('deleted_at',null)
                ->where('engineering',$engineering)
                ->select('enter_time','type','engineering','exit_time', DB::raw('count(*) as num'))
                ->groupBy('enter_time','exit_time','type')->orderBy('enter_time','desc')->get()->map(function ($value) {return (array)$value;})->toArray();
        }


        foreach ($res['subjects'] as $key => $value){

            if($value['type'] == 1){
                $res['subjects'][$key]['time'] = $value['enter_time'];
            }
            if($value['type'] == 2){
                $res['subjects'][$key]['time'] = $value['exit_time'];
            }
            if($value['type'] == 3){
                $res['subjects'][$key]['time'] = $value['enter_time'];
            }
            if($value['type'] == 4){
                $res['subjects'][$key]['time'] = $value['exit_time'];
            }

        }

        return $res;
    }


    public function workUserStatisticsMoon(Request $request)
    {
        $data = $request->all();
        $moon = $data['moon'];

        if(empty($moon)){
            $moon = Carbon::now();
        }

        $date = Carbon::createFromDate($moon);
        $startOfMonth = $date->startOfMonth()->toDateTimeString(); //该月第一天
        $endOfMonth = $date->endOfMonth()->toDateTimeString(); //该月最后一天

        $shangYue = DB::table('work_user_history')
                ->whereIn('type',[1,3])
                ->where('enter_time','<',$startOfMonth)->where('deleted_at',null)
                ->select('engineering',DB::raw('IFNULL(count(*),0) as shangYue'))
                ->groupBy('engineering')->get()->map(function ($value) {return (array)$value;})->toArray();

        $benYue = DB::table('work_user_history')
                ->whereIn('type',[1,3])
                ->where('enter_time','<',$endOfMonth)->where('deleted_at',null)
                ->select('engineering',DB::raw('IFNULL(count(*),0) as benYue'))
                ->groupBy('engineering')->get()->map(function ($value) {return (array)$value;})->toArray();

        $xinJin = DB::table('work_user_history')
                ->whereIn('type',[1,3])
                ->whereBetween('enter_time',[$startOfMonth,$endOfMonth])->where('deleted_at',null)
                ->select('engineering',DB::raw('IFNULL(count(*),0) as xinJin'))
                ->groupBy('engineering')->get()->map(function ($value) {return (array)$value;})->toArray();

        $liTui = DB::table('work_user_history')
            ->where('type',2)
            ->whereBetween('exit_time',[$startOfMonth,$endOfMonth])->where('deleted_at',null)
            ->select('engineering',DB::raw('IFNULL(count(*),0) as liTui'))
            ->groupBy('engineering')->get()->map(function ($value) {return (array)$value;})->toArray();

        $res['subjects'] = [];
        foreach ($shangYue as $key => $value){
            $res['subjects'][$key]['engineering'] = $value['engineering'];
            $res['subjects'][$key]['shangYue'] = $value['shangYue'];
            $res['subjects'][$key]['benYue'] = 0;
            $res['subjects'][$key]['xinJin'] = 0;
            $res['subjects'][$key]['liTui'] = 0;

            foreach ($xinJin as $k => $v){
                if($value['engineering'] == $v['engineering']){
                    $res['subjects'][$key]['xinJin'] = $v['xinJin'];
                }
            }

            foreach ($benYue as $k => $v){
                if($value['engineering'] == $v['engineering']){
                    $res['subjects'][$key]['benYue'] = $v['benYue'];
                }
            }

            foreach ($liTui as $k => $v){
                if($value['engineering'] == $v['engineering']){
                    $res['subjects'][$key]['liTui'] = $v['liTui'];
                    $res['subjects'][$key]['benYue'] = $res['subjects'][$key]['benYue'] - $v['liTui'];
                }
            }
        }


        return $res;
    }




}
